home *** CD-ROM | disk | FTP | other *** search
- Path: cs.mu.OZ.AU!bounce-back
- From: ldh@interlog.com (Laurent-David Hasson)
- Newsgroups: comp.std.c++,comp.lang.c++
- Subject: Class instanciation and virtual stuff..
- Date: 20 Apr 96 03:52:59 GMT
- Organization: Discobole consulting
- Approved: fjh@cs.mu.oz.au
- Message-ID: <4l6vtu$qap@steel.interlog.com>
- NNTP-Posting-Host: munta.cs.mu.oz.au
- X-Original-Date: Fri, 19 Apr 96 03:05:05 GMT
- X-Newsreader: News Xpress 2.0 Beta #0
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBFAgUBMXhf7eEDnX0m9pzZAQHTigF/bZKJ0ZnOtOUFvGhEF2JP2keqph5l7rkJ
- Cvj2KHTxHDPy2S0byuI7uD0H2EwJGyyV
- =MUAm
- Originator: fjh@munta.cs.mu.OZ.AU
-
- I have a question about how objects are constructed, and when "virtual"
- services become available.
-
- I remember reading in the ARM that one should not call "virtual" functions
- from a constructor. The reason being that an object should not be considered
- fully initialized until the last constructor had ended.
-
- However, by mistake, in one of my program, i was calling a library function
- which is calling virtual functions. I never noticed the error because
- apparently, the compiler i was using at the time allowed this behavior to
- work properly. However, i recently got a new version of the compiler, and
- everything broke. It took me several days to realize what was happening.
-
- I am now left with doubts with respect to the availibility of "virtual"
- services, and i am now wondering why it is the way it is, that is, why a
- class type should not be known, and all the plumbing in place, before the
- first constructor is called. Naively, i would assume that instanciating an
- object would go as follows:
- - Allocate the memory for the whole class
- - Create the suporting structural stuff (like typeID, VT etc...)
- - Then call the constructor, which follows with the Yo-Yo construction
- to initialize all the data members and whatever.
-
- This makes sense to me because the type of the class would actualy be known
- in the constructor of the base class. Instead, calling "typeid" on "this" in
- the base constructor gets the base class' typename. I understand why one
- should not lightly call a virtual function because the local vars would not
- have been initialized yet. But what if i called a virtual function that did
- initialize vars it needed (never access a var of the class before it has been
- written to first)? Or a virtual function that does not use the local
- members? Why should that fail?
-
- This leads me to think that all the "virtual" things are actually taken care
- of AFTER all construction has been made. And i do not understand why this
- is. Are there important penalties i do not understand, or pitfalls i do not
- see?
-
-
-
- Thanks,
- Laurent.
-
-
- PS: CC by email is much appreciated.
-
- ============================================================================
- Microsoft Network, or any service owned in full or in part by Microsoft, is
- prohibited from redistributing this work in any form, in whole or in part.
- Copyright, Laurent Hasson, 1996. License to distribute this post is
- available to Microsoft for US$10,000 per instance. Posting without
- permission constitutes an agreement to these terms. Please send notices of
- violation to ldh@interlog.com and postmaster@microsoft.com
- ============================================================================
- ---
- [ comp.std.c++ is moderated. To submit articles: try just posting with ]
- [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
- [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
- [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
- [ Comments? mailto:std-c++-request@ncar.ucar.edu ]
-